﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains("fguid"))
            {
                Response.Redirect("Login.aspx?prev=Account.aspx");
            }

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            long userid = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                List<UserInfo> userList = dataContext.UserInfos.Where(obj => (obj.UserID == userid)).ToList();
                if (userList.Count > 0)
                {
                    imgPhoto.ImageUrl = string.Format("~/UserImage/{0}", userList[0].ImageUrl);
                    txtName.Text = userList[0].FullName;
                    ddlGender.SelectedValue = userList[0].Gender;
                    txtCountry.Text = userList[0].Country;
                }
            }
        }

        protected void btnUpdateInfo_Click(object sender, EventArgs e)
        {
            try
            {
                long userid = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    dataContext.UpdateUserInfo(userid, txtName.Text, ddlGender.SelectedValue, txtCountry.Text, txtEmailID.Text);
                }

                ScriptManager.RegisterStartupScript(pnlUpdateInfo, this.GetType(), "alert", "alert('Your information is updated successfully')", true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(pnlUpdateInfo, this.GetType(), "alert", "alert('Error while updating.')", true);
            }
        }

        protected void btnUpdatePwd_Click(object sender, EventArgs e)
        {
            try
            {
                long userid = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    dataContext.UpdateUserPassword(userid, txtPassword.Text);
                }

                ScriptManager.RegisterStartupScript(pnlUpdatePassword, this.GetType(), "alert", "alert('Password updated sucessfully')", true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(pnlUpdatePassword, this.GetType(), "alert", "alert('Error while updating.')", true);
            }
        }

        protected void btnUploadPic_Click(object sender, EventArgs e)
        {
            try
            {
                long userid = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
                if (filePhoto.HasFile)
                {
                    string picfile = string.Format("{0}.png", Guid.NewGuid().ToString().Replace("-",""));
                    string folderPath = Server.MapPath("~/UserImage");
                    filePhoto.SaveAs(string.Format("{0}/{1}", folderPath, picfile));
                    using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                    {
                        dataContext.UpdateUserImage(userid, picfile);
                    }

                    imgPhoto.ImageUrl = string.Format("~/UserImage/{0}", picfile);
                    ScriptManager.RegisterStartupScript(pnlUpdatePassword, this.GetType(), "alert", "alert('Your profile pic is updated sucessfully')", true);
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(pnlUpdatePassword, this.GetType(), "alert", "alert('Error while updating.')", true);
            }
        }
    }
}